import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', () => {
    const isLogin = ref(localStorage.getItem('userInfo') ? true : false);

    const userInfo = ref(localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {
        userId: '',
        avatar: '',
        nickname: '',
        signature: '',
        collectCount: 0,
        favoriteCount: 0,
        followCount: 0,
        followerCount: 0,
        workCound: 0,
    })

    function reSetUser() {
        isLogin.value = false;
        userInfo.value.token = '';
        userInfo.value.userId = '';
        userInfo.value.avatar = '';
        userInfo.value.nickname = '';
        userInfo.value.signature = '';
        userInfo.value.collectCount = 0;
        userInfo.value.favoriteCount = 0;
        userInfo.value.followCount = 0;
        userInfo.value.followerCount = 0;
        userInfo.value.workCount = 0;
        localStorage.removeItem('userInfo');
    }

    function setUserInfo(user) {
        userInfo.value.token = user.token;
        userInfo.value.userId = user.userId;
        userInfo.value.avatar = user.avatar;
        userInfo.value.nickname = user.nickname;
        userInfo.value.signature = user.signature;
        userInfo.value.collectCount = user.collectCount;
        userInfo.value.favoriteCount = user.favoriteCount;
        userInfo.value.followCount = user.followCount;
        userInfo.value.followerCount = user.followerCount;
        userInfo.value.workCount = user.workCount;
        localStorage.setItem('userInfo', JSON.stringify(userInfo.value));
    }
    function setIsLogin(isLogin) {
        this.isLogin = isLogin;
    }

    return { isLogin, userInfo, reSetUser, setUserInfo, setIsLogin }
})